home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: mxsld2.pd.infn.it!LORETI
- From: loreti@mxsld2.pd.infn.it (Maurizio Loreti)
- Subject: Re: The size of a file
- X-Nntp-Posting-Host: mxsld2.pd.infn.it
- Message-ID: <DM85t1.2rq@news.cern.ch>
- Sender: news@news.cern.ch (USENET News System)
- Reply-To: loreti@mxsld2.pd.infn.it
- Organization: I.N.F.N. Padova - CDF/CMS VAXcluster
- References: <4ebc03$4gv@gate.compart.fi>,<11a7cc$142013.177@news.comet.net>
- Date: Sun, 4 Feb 1996 00:10:10 GMT
-
- In article <11a7cc$142013.177@news.comet.net>, mwilliams1@comet.net writes:
- >In <4ebc03$4gv@gate.compart.fi>, Fredrik Sandstrom <fred@spider.compart.fi> writes:
- >>This is driving me nuts. I can't find a simple way to get the size of a
- >>disk file. This must be possible using the standard C library, but I
- >>haven't figured out how!
- >
- >#include <stdio.h>
- >
- >int main(int argc, char *argv[]) {
- > FILE *fptr;
- > fpos_t length;
- > fptr=fopen(argv[1],"r");
-
- You should check if an argv[1] exists at all...
-
- > if (fptr==NULL) {
- > printf("Argh! Can't open '%s' !\n",argv[1]);
- > exit(-1);
-
- #include `stdlib.h> if you need exit(); and, -1 is platform dependent;
- the ANSI/ISO defined values are 0, EXIT_SUCCESS and EXIT_FAILURE.
-
- > }
- > fseek(fptr,0,2);
- > length=ftell(fptr);
-
- ftell returns a long, not a fpos_t. fpos_t's are used by fgetpos()
- and fsetpos(). Declare length as a long.
-
- > printf("The size of '%s' is %ld bytes\n",argv[1],length);
-
- The %ld is correct for a long; a fpos_t should be casted to (unsigned
- long) and printed as such.
-
- > fclose(fptr);
- > return(0);
- >}
-
- Apart from that, I realize that you missed Q/A 19.12 reading the FAQ
- list before posting:
-
- 19.12: How can I find out the size of a file, prior to reading it in?
-
- A: If the "size of a file" is the number of characters you'll be
- able to read from it in C, it is difficult or impossible to
- determine this number exactly).
-
- Under Unix, the stat() call will give you an exact answer.
- Several other systems supply a Unix-like stat() which will give
- an approximate answer. You can fseek() to the end and then use
- ftell(), but these tend to have the same problems: fstat() is
- not portable, and generally tells you the same thing stat()
- tells you; ftell() is not guaranteed to return a byte count
- except for binary files. Some systems provide routines called
- filesize() or filelength(), but these are not portable, either.
-
- Are you sure you have to determine the file's size in advance?
- Since the most accurate way of determining the size of a file as
- a C program will see it is to open the file and read it, perhaps
- you can rearrange the code to learn the size as it reads.
-
- References: ANSI Sec. 4.9.9.4; ISO Sec. 7.9.9.4; H&S
- Sec. 15.5.1; PCS Sec. 12 p. 213; POSIX Sec. 5.6.2.
- --
- Maurizio Loreti http://mvxpd5.pd.infn.it/wwwcdf/mlo.html
- Un. of Padova, Dept. of Physics - Padova, Italy loreti@padova.infn.it
-